#include "fstats.h"

date parseDate (string& tempLine) 
{
	date tempDate;
	unsigned short int gap1, gap2;
	gap1 = tempLine.find(".");
	gap2 = tempLine.rfind(".");
	tempDate.day = atoi(tempLine.substr(0, gap1).c_str());
	tempDate.month = atoi(tempLine.substr(gap1+1, (gap2 - gap1 - 1)).c_str());
	tempDate.year = atoi(tempLine.substr(gap2+1, tempLine.size()).c_str());
	return tempDate;
}	

match parseScore (string& tempLine, match& tempMatch)
{
	tempMatch.homeScore = atoi(tempLine.substr(0,1).c_str());
	tempMatch.visitorScore = atoi(tempLine.substr(2,1).c_str());
	if (tempMatch.homeScore > tempMatch.visitorScore) tempMatch.result = 1;
	else if(tempMatch.homeScore < tempMatch.visitorScore) tempMatch.result = -1;
	else tempMatch.result = 0;
	return tempMatch;
} 

void parsing (vector <match>& matchList, ifstream &input_file)
{
	string tempLine, lineWithNoTourAndScore, lineScore, lineDate;
	match tempMatch;
	unsigned short int tab[4];
	do 
	{
		getline (input_file, tempLine);
		if (tempLine.find('!') != tempLine.npos) flushDynamicalArraysAndStoreInfo();
		else 
		{
			tab[0] = tempLine.find('\t');
			tab[3] = tempLine.rfind('\t');
			lineScore = tempLine.substr((tab[3]+1), 3);
			parseScore (lineScore, tempMatch); //we got score 
			lineWithNoTourAndScore = tempLine.substr((tab[0]+1), (tab[3] - tab[0] - 1));
			tab[1] = lineWithNoTourAndScore.find('\t');
			tab[2] = lineWithNoTourAndScore.rfind('\t');
			lineDate = lineWithNoTourAndScore.substr(0, tab[1]);
			tempMatch.matchDate = parseDate(lineDate);
			tempMatch.homeName = lineWithNoTourAndScore.substr(tab[1]+1, (tab[2]-tab[1]-1));
			tempMatch.visitorName = lineWithNoTourAndScore.substr(tab[2]+1, lineWithNoTourAndScore.size()-tab[2]);
			matchList.push_back(tempMatch);
		}
	} while (input_file.peek() != EOF);	
}

//function for printing the vectors out
void printing (vector <vector <resultingMatrixCell> > & resultingMatrix, ofstream& output_file)
{
	unsigned short size = resultingMatrix[0].size();
	output_file << "i	j	GP	HW	D	VW	HW%	D%	VW%" << endl;
	for(unsigned short i = 0; i < size; i++) //Put into the file final vectors...
	{
		for(unsigned short j = 0; j < size; j++)
		{
			output_file << i << '\t' << j 
			<< '\t' << setiosflags(ios::fixed) << setprecision(0) <<resultingMatrix[i][j].gamePlayed //Integer values
			<< '\t' << resultingMatrix[i][j].homeWins 
			<< '\t' << resultingMatrix[i][j].draws 
			<< '\t' << resultingMatrix[i][j].visitorWins
			<< '\t' << setiosflags(ios::fixed) << setprecision(2) <<resultingMatrix[i][j].homeWinsNorm //Frequencies
			<< '\t' << resultingMatrix[i][j].drawsNorm 
			<< '\t' << resultingMatrix[i][j].visitorWinsNorm
			<< endl;
		}
	}
}

